package com.hundsun.documentripper.service.impl;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hundsun.documentripper.domain.dto.DemoSource;
import com.hundsun.documentripper.domain.pojo.Demo;
import com.hundsun.documentripper.domain.pojo.DemoNode;
import com.hundsun.documentripper.domain.query.DemoQuery;
import com.hundsun.documentripper.mapper.DemoMapper;
import com.hundsun.documentripper.mapper.DemoNodeMapper;
import com.hundsun.documentripper.service.DemoService;
import com.hundsun.documentripper.service.FileNodeService;
import com.hundsun.documentripper.service.NodeContentService;
import com.hundsun.documentripper.service.NodeTemplateService;
import com.hundsun.documentripper.utils.StringUtil;

/**
 * Note：
 *
 * @author ：mengjw
 * @description：预设模板服务实现
 * @date ：Created in 2021/1/30
 */
@Service
public class DemoServiceImpl implements DemoService {

    @Autowired
    private DemoMapper demoMapper;
    @Autowired
    private DemoNodeMapper demoNodeMapper;
    @Autowired
    private FileNodeService fileNodeService;
    @Autowired
    private NodeTemplateService nodeTemplateService;
    @Autowired
    private NodeContentService nodeContentService;

    @Override
    public Demo insertDemo(Demo demo) {
        demo.setUuid(UUID.randomUUID().toString());
        demoMapper.insert(demo);
        return demo;
    }

    @Override
    public void updateDemo(Demo demo) {
        demoMapper.updateById(demo);
    }

    @Override
    public void deleteDemo(String uuid) {
        demoMapper.deleteById(uuid);
        QueryWrapper<DemoNode> wrapper = new QueryWrapper<>();
        wrapper.eq("demo_uuid", uuid);
        demoNodeMapper.delete(wrapper);
    }

    @Override
    public IPage<Demo> getDemoListByPage(DemoQuery demoQuery) {
        QueryWrapper<Demo> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        wrapper.like(StringUtil.isNotEmpty(demoQuery.getUuid()), "uuid", demoQuery.getUuid());
        wrapper.like(StringUtil.isNotEmpty(demoQuery.getName()), "name", demoQuery.getName());
        return demoMapper.selectPage(new Page<Demo>(demoQuery.getCurrent(), demoQuery.getSize()), wrapper);
    }

    @Override
    public DemoSource getDemoSourceById(String uuid) {
        DemoSource demoSource = DemoSource.formDemo(demoMapper.selectById(uuid));
        QueryWrapper<DemoNode> wrapper = new QueryWrapper<>();
        wrapper.eq("demo_uuid", uuid);
        demoSource.setDemoNodes(demoNodeMapper.selectList(wrapper));
        // 获取依赖的节点模板列表和内容列表
        fileNodeService.getNodeTemplateViewsAndNodeContents(demoSource, demoSource.getDemoNodes());
        return demoSource;
    }

    @Override
    public DemoSource saveDemoSource(DemoSource demoSource) {
        // 更新预设模板
        demoMapper.updateById(demoSource);
        // 删除老的节点
        QueryWrapper<DemoNode> wrapper = new QueryWrapper<>();
        wrapper.eq("demo_uuid", demoSource.getUuid());
        demoNodeMapper.delete(wrapper);
        // 插入新的文件节点
        if (null != demoSource.getDemoNodes() && !demoSource.getDemoNodes().isEmpty()) {
            demoSource.getDemoNodes().forEach(o ->
            {
                o.setDemoUuid(demoSource.getUuid());
                o.setCreateUser(demoSource.getCreateUser());
            });
            demoNodeMapper.insertBatchSomeColumn(demoSource.getDemoNodes());
        }
        // 插入新的节点样式
        nodeTemplateService.saveNodeTemplateViewList(demoSource.getNodeTemplateViews());
        if (null != demoSource.getNodeContents() && !demoSource.getNodeContents().isEmpty()) {
            // 插入新的内容
            nodeContentService.insertNodeContentBatch(demoSource.getNodeContents());
        }
        // 全部处理完成后，获取依赖的节点模板列表和内容列表，返回全量的文件元数据
        fileNodeService.getNodeTemplateViewsAndNodeContents(demoSource, demoSource.getDemoNodes());
        return demoSource;
    }
}